----------------------------
-- Simplistic Street
-- by Jammie Dodger
--
-- Project Started - October 20, 2009
-- Completion -None
----------------------------

local draw = draw
local surface = surface

surface.CreateFont( "TargetID", 22, 800, true, false, "FontSmall" );
surface.CreateFont( "TargetID", 16, 800, true, false, "FontSmallX2" );

function GetScreenCenterBounce( ent, bounce )
	local pos = ent:LocalToWorld(ent:OBBCenter() + Vector(0,0,10)):ToScreen()

	return pos.x, pos.y + ( math.sin( CurTime() ) * ( bounce or 8 ) )
end

function FindPlayerUniqueName(info)

	local pls = player.GetAll()

	for k, v in pairs(pls) do
		if info == v:UniqueID() then
			return v:Nick()
		end
	end
end

function FindUniqueNamePlayer(info)

	local pls = player.GetAll()
	
	for k, v in pairs(pls) do
		if string.find(info, v:Nick()) then
			return v:UniqueID()
		end
	end
end

function PaintPaintPaint()

	local ply = LocalPlayer()
	
	if ( ply:Alive() ) then
	
		local tr = ply:GetEyeTrace()
		local Distance = 200
		
		if ValidEntity(tr.Entity) then
			if ( tr.Entity:IsDoor()) then
				local Alpha = math.Clamp( 255 - ( ( 255 / Distance ) * tr.Entity:GetPos():Distance( ply:GetShootPos() ) ), 0, 255 )
				local x, y = GetScreenCenterBounce(tr.Entity)
			
				if tr.Entity:IsUnownable() then
					draw.SimpleText("Unable to own", "FontSmall", x, y, Color( 255, 0, 0, Alpha ), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
					
				elseif tr.Entity:IsCopOnly() then
					draw.SimpleText("Authorised Personel Only", "FontSmall", x, y, Color( 255, 0, 0, Alpha ), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
					
				elseif !tr.Entity:IsCopOnly() and !tr.Entity:IsUnownable() then
					if tr.Entity:GetNWString( "doorname" ) == "" or tr.Entity:GetNWString( "doorname" ) == nil then
						draw.SimpleText("Unowned", "FontSmall", x, y, Color( 255, 0, 0, Alpha ), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
						draw.SimpleText("Press F4 to purchase", "FontSmallX2", x, y + 20, Color( 255, 55, 55, Alpha ), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
					else
						draw.SimpleText(tr.Entity:GetNWString( "doorname" ), "FontSmall", x, y, Color( 255, 0, 0, Alpha ), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
						
					if LocalPlayer() == tr.Entity.Owners then	
						draw.SimpleText("Press F3 to access menu", "FontSmallX2", x, y + 20, Color( 255, 55, 55, Alpha ), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
						if tr.Entity.CoOwners and tr.Entity.CoOwners != nil then
							draw.SimpleText("Also owned by:", "FontSmallX2", x, y + 40, Color( 255, 0, 0, Alpha ), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
				
							local Number = #tr.Entity.CoOwners
								for i=1,Number do
									if i == 1 then
										y = y + 60
									else
										y = y + 20
									end
						
								local Name = FindPlayerUniqueName(tostring(tr.Entity.CoOwners[i]))
									draw.SimpleText(Name, "FontSmallX2", x, y, Color( 255, 0, 0, Alpha ), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
					
											end
										end
										
										
									else
							
								if tr.Entity.CoOwners and tr.Entity.CoOwners != nil then
									draw.SimpleText("Also owned by:", "FontSmallX2", x, y + 20, Color( 255, 0, 0, Alpha ), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
				
								local Number = #tr.Entity.CoOwners
									for i=1,Number do
										if i == 1 then
											y = y + 40
										else
											y = y + 20
										end
						
								local Name = FindPlayerUniqueName(tostring(tr.Entity.CoOwners[i]))
									draw.SimpleText(Name, "FontSmallX2", x, y, Color( 255, 0, 0, Alpha ), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
								end
							end
						end
					end
				end
			end
		end
	end
end

function DoorMenu(ply)

	local frame = vgui.Create( "DFrame" )
		frame:SetTitle( "Universal Door Menu" )
		frame:SetSize( 375, 150 )
		frame:SetSizable( false )
		frame:Center()
		frame:MakePopup()
		
	local panel = vgui.Create( "DPanel", frame )
		panel:SetSize( frame:GetWide() - 10, frame:GetTall() - 30 )
		panel:SetPos( 5, 25 )
		function panel.Paint() 
			surface.SetDrawColor( 0, 0, 0, 235 )
			surface.DrawRect( 0, 0, panel:GetWide(), panel:GetTall() )
		end
		
	local text = vgui.Create( "DLabel", frame )
		text:SetText( "Welcome to the UDM - Universal Door Menu." )
		text:SizeToContents()
		text:SetPos( ((frame:GetWide() / 2) - (text:GetWide() / 2 )), 35 )
		text:SetColor( Color( 255, 255, 255, 175 ) )
		
	local text2 = vgui.Create( "DLabel", frame )
		text2:SetText( "What would you like to do today?" )
		text2:SizeToContents()
		text2:SetPos( ((frame:GetWide() / 2) - (text2:GetWide() / 2 )), 55 )
		text2:SetColor( Color( 255, 255, 255, 175 ) )

	local button = vgui.Create( "DButton" )
		button:SetParent( frame )
		button:SetText( "Add a Co-Owner" )
		button:SetPos( 25, 100 )
		button:SetSize( 150, 20 )
		button.DoClick = function()
			AddCoOwner()
			frame:Close()
		end
	 
	local button2 = vgui.Create( "DButton" )
		button2:SetParent( frame )
		button2:SetText( "Remove a Co-Owner" )
		button2:SetPos( 200, 100 )
		button2:SetSize( 150, 20 )
		button2.DoClick = function()
			RemoveCoOwner()
			frame:Close()
		end
end

function AddCoOwner()

local tr = LocalPlayer():GetEyeTrace()

	local AddFrame = vgui.Create( "DFrame" )
		AddFrame:SetTitle( "Add a Co-Owner" )
		AddFrame:SetSize( 375, 180 )
		AddFrame:SetSizable( false )
		AddFrame:Center()
		AddFrame:MakePopup()
		
	local AddPanel = vgui.Create( "DPanel", AddFrame )
		AddPanel:SetSize( AddFrame:GetWide() - 10, AddFrame:GetTall() - 30 )
		AddPanel:SetPos( 5, 25 )
		function AddPanel.Paint() 
			surface.SetDrawColor( 0, 0, 0, 235 )
			surface.DrawRect( 0, 0, AddPanel:GetWide(), AddPanel:GetTall() )
		end
		
	local AddText1 = vgui.Create( "DLabel", AddFrame )
		AddText1:SetText(  "Thank you for selecting the option \"Add a Co-Owner\"" )
		AddText1:SizeToContents()
		AddText1:SetPos( ((AddFrame:GetWide() / 2) - (AddText1:GetWide() / 2 )), 35 )
		AddText1:SetColor( Color( 255, 255, 255, 175 ) )
		
	local AddText2 = vgui.Create( "DLabel", AddFrame )
		AddText2:SetText( "Who would you like to add?" )
		AddText2:SizeToContents()
		AddText2:SetPos( ((AddFrame:GetWide() / 2) - (AddText2:GetWide() / 2 )), 55 )
		AddText2:SetColor( Color( 255, 255, 255, 175 ) )
		
	local AddBox = vgui.Create( "DMultiChoice", AddFrame )
	AddBox:SetSize( 236, 20 )
	AddBox:SetPos( ((AddFrame:GetWide() / 2) - (AddBox:GetWide() / 2 )), 90 )
	AddBox:SetEditable( false )
	for k,v in pairs(player.GetAll()) do
		if tr.Entity.CoOwners and !table.HasValue(tr.Entity.CoOwners, v:UniqueID()) and v != LocalPlayer() then
			AddBox:AddChoice(v:Nick())
		end
		if !tr.Entity.CoOwners and v != LocalPlayer() then
			AddBox:AddChoice(v:Nick())
		end
	end
	
	local AddButton = vgui.Create( "DButton" )
		AddButton:SetParent( AddFrame )
		AddButton:SetText( "Add Co-Owner" )
		AddButton:SetPos( 25, 140 )
		AddButton:SetSize( 150, 20 )
		AddButton.DoClick = function()
		if AddBox.TextEntry:GetValue() != nil or "" then
		local Thing = FindUniqueNamePlayer(AddBox.TextEntry:GetValue())
			if Thing and Thing != "" or nil then
				LocalPlayer():ConCommand("ss_addcoowner "..Thing.." "..AddBox.TextEntry:GetValue())
				AddFrame:Close()
			else
				chat.AddText(ply, Color(100,149,237,255), "Error! That player does not exist!")
			end
		end
	end
	 
	local AddButton2 = vgui.Create( "DButton" )
		AddButton2:SetParent( AddFrame )
		AddButton2:SetText( "Cancel" )
		AddButton2:SetPos( 200, 140 )
		AddButton2:SetSize( 150, 20 )
		AddButton2.DoClick = function()
			AddFrame:Close()
	end	
end

function RemoveCoOwner()

local tr = LocalPlayer():GetEyeTrace()

	local RemoveFrame = vgui.Create( "DFrame" )
		RemoveFrame:SetTitle( "Remove a Co-Owner" )
		RemoveFrame:SetSize( 375, 180 )
		RemoveFrame:SetSizable( false )
		RemoveFrame:Center()
		RemoveFrame:MakePopup()
		
	local RemovePanel = vgui.Create( "DPanel", RemoveFrame )
		RemovePanel:SetSize( RemoveFrame:GetWide() - 10, RemoveFrame:GetTall() - 30 )
		RemovePanel:SetPos( 5, 25 )
		function RemovePanel.Paint() 
			surface.SetDrawColor( 0, 0, 0, 235 )
			surface.DrawRect( 0, 0, RemovePanel:GetWide(), RemovePanel:GetTall() )
		end
		
	local RemoveText = vgui.Create( "DLabel", RemoveFrame )
		RemoveText:SetText(  "Thank you for selecting the option \"Remove a Co-Owner\"" )
		RemoveText:SizeToContents()
		RemoveText:SetPos( ((RemoveFrame:GetWide() / 2) - (RemoveText:GetWide() / 2 )), 35 )
		RemoveText:SetColor( Color( 255, 255, 255, 175 ) )
		
	local RemoveText2 = vgui.Create( "DLabel", RemoveFrame )
		RemoveText2:SetText( "Who would you like to remove?" )
		RemoveText2:SizeToContents()
		RemoveText2:SetPos( ((RemoveFrame:GetWide() / 2) - (RemoveText2:GetWide() / 2 )), 55 )
		RemoveText2:SetColor( Color( 255, 255, 255, 175 ) )
		
	local RemoveBox = vgui.Create( "DMultiChoice", RemoveFrame )
	RemoveBox:SetSize( 236, 20 )
	RemoveBox:SetPos( ((RemoveFrame:GetWide() / 2) - (RemoveBox:GetWide() / 2 )), 90 )
	RemoveBox:SetEditable( false )
	for k,v in pairs(player.GetAll()) do
		if tr.Entity.CoOwners and table.HasValue(tr.Entity.CoOwners, v:UniqueID()) and v != LocalPlayer() then
			RemoveBox:AddChoice(v:Nick())
		end
	end
	
	local RemoveButton = vgui.Create( "DButton" )
		RemoveButton:SetParent( RemoveFrame )
		RemoveButton:SetText( "Remove Co-Owner" )
		RemoveButton:SetPos( 25, 140 )
		RemoveButton:SetSize( 150, 20 )
		RemoveButton.DoClick = function()
		if RemoveBox.TextEntry:GetValue() != nil or "" then
		local Thing = FindUniqueNamePlayer(RemoveBox.TextEntry:GetValue())
			if Thing and Thing != "" or nil then
					if table.Count(tr.Entity.CoOwners) == 1 then
						tr.Entity.CoOwners = nil
						LocalPlayer():ConCommand("ss_removecoowner "..Thing.." "..RemoveBox.TextEntry:GetValue())
						RemoveFrame:Close()
					elseif table.Count(tr.Entity.CoOwners) > 1 then
						table.remove(tr.Entity.CoOwners, Thing)
						RemoveFrame:Close()
				end
			else
				chat.AddText(ply, Color(100,149,237,255), "Error! That player does not exist!")
			end
		end
	end
	 
	local RemoveButton2 = vgui.Create( "DButton" )
		RemoveButton2:SetParent( RemoveFrame )
		RemoveButton2:SetText( "Cancel" )
		RemoveButton2:SetPos( 200, 140 )
		RemoveButton2:SetSize( 150, 20 )
		RemoveButton2.DoClick = function()
			RemoveFrame:Close()
		end	
	
end

hook.Add("HUDPaint", "PaintGalore", PaintPaintPaint)
concommand.Add("ss_doormenu", DoorMenu)